using System;
using System.Reflection.Metadata;
using Godot;

[GlobalClass]
public partial class CSkill : Resource
{
    [ExportGroup("ID")]
    [Export] public SKILL_ID mID;

    [ExportGroup("施放距离")]
    [Export] public float mUseDis;
    [ExportGroup("使用动画")]
    [Export] public ANI_TYPE mUseAni = ANI_TYPE.ATTACK;
    [ExportGroup("施放目标")]
    [Export] public SKILL_TARGET mPutTarget = SKILL_TARGET.NO_TARGET;
    [Export] public CAniEvent mAniEvent = new();

    public virtual CSkill copy()
    {
        CSkill re = Duplicate() as CSkill;
        re.mAniEvent = mAniEvent.copy();
        return re;
    }
    public void resetAniEvent(NCharacter cha)
    {
        if (mPutTarget == SKILL_TARGET.ENEMY_TARGET)
        {
            NCharacter enemy = cha.GetNodeOrNull<NCharacter>(cha.mData.mSkillTarget.mTargetPath);
            cha.mData.mDir = CHelp.checkDir(cha.GlobalPosition, enemy.GlobalPosition);
        }
        mAniEvent.mTrigged = false;

        //重置攻击动画
        cha.Ani.Frame = 0;
        cha.Ani.FrameProgress = 0;
        cha.Ani.Animation = cha.aniName(ANI_TYPE.ATTACK);
        //设为非循环
        cha.Ani.SpriteFrames.SetAnimationLoop(cha.aniName(ANI_TYPE.ATTACK), false);
        cha.Ani.Play();
    }
    public virtual void calHurt(NCharacter attackCha, NCharacter beAttackCha)
    {
        beAttackCha.mData.mLife -= 1;

        beAttackCha.beAttack(attackCha);
    }
    public virtual bool canDoHurt(NCharacter cha)
    {
        if (cha.Ani.Frame == mAniEvent.mFrame && cha.Ani.FrameProgress >= mAniEvent.mFrameProcess && !mAniEvent.mTrigged)
        {
            mAniEvent.mTrigged = true;
            return true;
        }
        return false;
    }
    public virtual bool isEnd(NCharacter cha)
    {
        //是否播放动画
        return !cha.Ani.IsPlaying();
    }

    public virtual void doHurt(NCharacter cha)
    {

    }
    public virtual SKILL_RE canPlay(NCharacter cha)
    {
        if (mPutTarget == SKILL_TARGET.NO_TARGET)
            return SKILL_RE.SUCCESS;
        if (mPutTarget == SKILL_TARGET.ENEMY_TARGET)
        {
            NCharacter targetCha = cha.GetNodeOrNull<NCharacter>(cha.mData.mSkillTarget.mTargetPath);
            if (targetCha == null || !CHelp.canBattle(targetCha, cha))
            {
                return SKILL_RE.NO_TARGET;
            }

            if (!CHelp.checkDis(cha, targetCha, mUseDis))
            {
                return SKILL_RE.NO_ENOUGH_DIS;
            }

            return SKILL_RE.SUCCESS;
        }

        return SKILL_RE.UNKNOW;
    }

}